package main

import "fmt"

// challenges -> 挑战

//this result in a deadlock
func deadLockChallenges() {
	c := make(chan int)
	c <- 1
	fmt.Println(<-c)
}

//solution method 1
func deadLockChallengesSolution1() {
	c := make(chan int, 1)
	c <- 1
	fmt.Println(<-c)
}

//solution method 2
func deadLockChallengesSolution2() {
	c := make(chan int, 1)
	go func() {
		c <- 1
	}()
	fmt.Println(<-c)
}

//only print 0
func deadLockChallenges2() {
	c := make(chan int)

	go func() {
		for i := 0; i < 10; i++ {
			c <- i
		}
	}()

	fmt.Println(<-c)
}

//for rang
func deadLockChallengesSolution3() {
	c := make(chan int)

	go func() {
		for i := 0; i < 10; i++ {
			c <- i
		}
		close(c)
	}()

	for v := range c {
		fmt.Println(v)
	}
}

func main() {
	//deadLockChallenges()
	//deadLockChallengesSolution1()
	//deadLockChallengesSolution2()
	//deadLockChallenges2()
	deadLockChallengesSolution3()
}
